package com.global.gbinventory.barang.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.TableModelListener;

import com.global.gbinventory.main.model.GBTableModel;


public class GrupFrame extends JInternalFrame {

	private static final long serialVersionUID = -8606519173264439783L;
	
	private JTable tableSubgrup;
	
	private JButton btnNew;
	private JButton btnEdit;
	private JButton btnDelete;
	private JButton btnSave;
	private JButton btnCancel;
	private JButton btnOkList;
	private JButton btnCancelList;
	
	private JComboBox cmbBoxChooseGrup;
	private JTextField txtFldChosenGrup;
	private JLabel lblGrupTable;
	private JLabel lblKeyNote;
		
	/**
	 * Create the frame.
	 */
	public GrupFrame() {
		//resizeable, closeable, maximizable, iconifiable
		super("Grup Barang", false, true, false, true);
		setBounds(100, 100, 613, 586);
				
//		this.groupController = groupController;
//		this.subgroupController = subgroupController;

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 20));


		GridLayout gl_panelNorth = new GridLayout(2,1);
		gl_panelNorth.setHgap(10);
		gl_panelNorth.setVgap(5);
		JPanel panelNorth = new JPanel(gl_panelNorth);
		panel.add(panelNorth, BorderLayout.NORTH);

		JPanel panelButtons = new JPanel();
		panelButtons.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelButtons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panelNorth.add(panelButtons);

		btnNew = new JButton("New Group");
		panelButtons.add(btnNew);

		btnEdit = new JButton("Edit Group");
		panelButtons.add(btnEdit);

		btnDelete = new JButton("Delete Group");
		panelButtons.add(btnDelete);

		btnSave = new JButton("Save");
		btnSave.setEnabled(false);
		panelButtons.add(btnSave);

		btnCancel = new JButton("Cancel");
		btnCancel.setEnabled(false);
		panelButtons.add(btnCancel);
		
		btnOkList = new JButton("Ok");
		panelButtons.add(btnOkList);
		
		btnCancelList = new JButton("Cancel");
		panelButtons.add(btnCancelList);

		FlowLayout fl_panelChooseGroup = new FlowLayout();
		fl_panelChooseGroup.setVgap(8);
		JPanel panelChooseGroup = new JPanel(fl_panelChooseGroup);
		panelChooseGroup.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelNorth.add(panelChooseGroup);

		JLabel lblChooseGroup = new JLabel("Pilih group barang : ");

		cmbBoxChooseGrup = new JComboBox();
		cmbBoxChooseGrup.setPreferredSize(new Dimension(200, 22));
		panelChooseGroup.add(lblChooseGroup);
		panelChooseGroup.add(cmbBoxChooseGrup);

		BorderLayout bl_panelCenter = new BorderLayout();
		bl_panelCenter.setVgap(20);
		JPanel panelCenter = new JPanel(bl_panelCenter);
		panel.add(panelCenter, BorderLayout.CENTER);

		JPanel panelChosenGroup = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10));
		panelChosenGroup.setBorder(new LineBorder(new Color(0, 0, 0)));
		JLabel lblChosenGroup = new JLabel("Group Barang : ");

		txtFldChosenGrup = new JTextField();
		txtFldChosenGrup.setEditable(false);
		txtFldChosenGrup.setColumns(18);
		panelChosenGroup.add(lblChosenGroup);
		panelChosenGroup.add(txtFldChosenGrup);
		panelCenter.add(panelChosenGroup, BorderLayout.NORTH);

		JPanel panelTable = new JPanel(new BorderLayout());
		panelTable.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelCenter.add(panelTable, BorderLayout.CENTER);

		JPanel leftPanel = new JPanel();
		panelTable.add(leftPanel, BorderLayout.WEST);

		JPanel rightPanel = new JPanel();
		panelTable.add(rightPanel, BorderLayout.EAST);

		JPanel upPanel = new JPanel();
		lblGrupTable = new JLabel();
		upPanel.add(lblGrupTable);
		panelTable.add(upPanel, BorderLayout.NORTH);

		JPanel bottomPanel = new JPanel();
		lblKeyNote = new JLabel("NB : Tekan tombol ** PANAH KANAN ** untuk menambah baris Sub-Group baru");
		bottomPanel.add(lblKeyNote);
		panelTable.add(bottomPanel, BorderLayout.SOUTH);

		tableSubgrup = new JTable();
		tableSubgrup.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableSubgrup.setFillsViewportHeight(true);

		JScrollPane scrollPane = new JScrollPane(tableSubgrup, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setBounds(0, 0, 469, 427);
		panelTable.add(scrollPane, BorderLayout.CENTER);

	}

	public void displayFrame(boolean state, boolean viewedOnly){
		if(viewedOnly){
			activateViewedOnlyButtons();
		}else{
			deactivateViewedOnlyButtons();
		}
		this.setVisible(state);
	}
	
	public void disposeFrame(){
		dispose();
	}
	
	public void requestFocusTxtFldChosenGrup(){
		txtFldChosenGrup.requestFocusInWindow();
	}
	
	public JTable getSubgrupTable(){
		return tableSubgrup;
	}
	
	public int getChosenGrupIndex(){
		return cmbBoxChooseGrup.getSelectedIndex();
	}
	
	public void setChosenGrupIndex(int index){
		cmbBoxChooseGrup.setSelectedIndex(index);
	}
	
	public Object getChosenGrupObject(){
		return cmbBoxChooseGrup.getSelectedItem();
	}
	
	public int getSubgrupTableSelectedRow(){
		return tableSubgrup.getSelectedRow();
	}
	
	public void setChooseGrupModel(DefaultComboBoxModel model){
		cmbBoxChooseGrup.setModel(model);
	}
	
	public void setTxtFldChosenGrup(String text){
		txtFldChosenGrup.setText(text);
	}
	
	public String getChosenGrup(){
		return txtFldChosenGrup.getText();
	}
	
	public void setLblGrupTable(String text){
		lblGrupTable.setText(text);
	}
	
	public void setEditableTxtFldChosenGrup(boolean b){
		txtFldChosenGrup.setEditable(b);
	}
	
	public void setEnabledButtonNew(boolean b){
		btnNew.setEnabled(b);
	}
	
	public void setEnabledButtonEdit(boolean b){
		btnEdit.setEnabled(b);
	}
	
	public void setEnabledButtonDelete(boolean b){
		btnDelete.setEnabled(b);
	}
	
	public void setEnabledButtonSave(boolean b){
		btnSave.setEnabled(b);
	}
	
	public boolean isButtonSaveEnabled(){
		return btnSave.isEnabled();
	}
	
	public void setEnabledButtonCancel(boolean b){
		btnCancel.setEnabled(b);
	}
	
	public void setEnabledTableSubgrup(boolean b){
		tableSubgrup.setEnabled(b);
	}

	public void setEnabledCmbBoxChooseGrup(boolean b){
		cmbBoxChooseGrup.setEnabled(b);
	}
	
	public void setSubgrupTableModel(GBTableModel tableModel){
		tableSubgrup.setModel(tableModel);
	}
	
	public void addSubgrupTableListener(TableModelListener l){
		tableSubgrup.getModel().addTableModelListener(l);
	}
	
	public void addSubgrupTableKeyListener(KeyListener l){
		tableSubgrup.addKeyListener(l);
	}
	
	public void addBtnNewListener(ActionListener l){
		btnNew.addActionListener(l);
	}
	
	public void addBtnEditListener(ActionListener l){
		btnEdit.addActionListener(l);
	}
	
	public void addBtnDeleteListener(ActionListener l){
		btnDelete.addActionListener(l);
	}
	
	public void addBtnSaveListener(ActionListener l){
		btnSave.addActionListener(l);
	}
	
	public void addBtnCancelListener(ActionListener l){
		btnCancel.addActionListener(l);
	}

	public void addcmbBoxChooseGrupListener(ActionListener l){
		cmbBoxChooseGrup.addActionListener(l);
	}
	
	public void addBtnOkListListener(ActionListener l){
		btnOkList.addActionListener(l);
	}
	
	public void addBtnCancelListListener(ActionListener l){
		btnCancelList.addActionListener(l);
	}
	
	private void activateViewedOnlyButtons(){
		btnOkList.setVisible(true);
		btnCancelList.setVisible(true);
		btnNew.setVisible(false);
		btnEdit.setVisible(false);
		btnSave.setVisible(false);
		btnCancel.setVisible(false);
		btnDelete.setVisible(false);
		
		lblKeyNote.setVisible(false);
		
	}
	
	private void deactivateViewedOnlyButtons(){
		btnOkList.setVisible(false);
		btnCancelList.setVisible(false);
		btnNew.setVisible(true);
		btnEdit.setVisible(true);
		btnSave.setVisible(true);
		btnCancel.setVisible(true);
		btnDelete.setVisible(true);
		
		lblKeyNote.setVisible(true);
	}
	
}
